package com.keyinong.jiashiyuan.view;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.keyinong.jiashiyuan.R;
import com.keyinong.jiashiyuan.tools.HttpTools;
import com.loopj.android.http.JsonHttpResponseHandler;

public class LoginActivity extends Activity {
	/**登录*/
	private Button btndl;
	/**图标*/
	private ImageView iv_logo,iv_yonghuming,iv_mima;
	/**手机号*/
	private EditText et_shoujihao;
	/**密码*/
	private EditText et_password;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		setView();
		btndl.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
				login();
			}
		});
	}
	private void setView() {
		iv_logo=(ImageView)findViewById(R.id.iv_logo);
		iv_yonghuming=(ImageView)findViewById(R.id.iv_yonghuming);
		iv_mima=(ImageView)findViewById(R.id.iv_mima);
		et_shoujihao=(EditText)findViewById(R.id.editText1);
		et_password=(EditText)findViewById(R.id.editText2);
		btndl=(Button)findViewById(R.id.btn_denglu);
	}
	/**登录*/
	private void login(){
		String phone=et_shoujihao.getText().toString();
		String pwd=et_password.getText().toString();
		if(TextUtils.isEmpty(phone)){
			et_shoujihao.setError("手机号为空");
			return;
		}
		if(TextUtils.isEmpty(pwd)){
			et_password.setError("密码为空");
			return;
		}
				if(!TextUtils.isDigitsOnly(phone)){
					et_shoujihao.setError("请输入正确的手机号");
					return;
				}
				if(phone.length()!=11){
					et_shoujihao.setError("手机号不符");
					return;
				}
//				
//				if(pwd.length()<6){
//					et_password.setError("密码不能小于6位");
//					return;
//				}
				HttpTools httptools=new HttpTools(getApplicationContext());
				httptools.login(phone, pwd, new JsonLogin());
	}
	private class JsonLogin extends JsonHttpResponseHandler{
		@Override
		public void onSuccess(int statusCode, Header[] headers,
				JSONObject response) {
			// TODO Auto-generated method stub
			super.onSuccess(statusCode, headers, response);
			try {
				boolean code=response.getBoolean("code");
				String msg=response.getString("msg");
//  			    Log.i("TAG", msg);
				if(code==true){
					Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
					JSONObject object=response.getJSONObject("data");
					String token=object.getString("token");
					String name=object.getString("name");
//					Log.i("shi", token);
					SharedPreferences spReg = getSharedPreferences("spReg",
							Context.MODE_PRIVATE);
					Editor editor = spReg.edit();
					editor.putString("token", token);
					editor.putString("name", name);
					editor.commit();
					
					SharedPreferences spuser = getSharedPreferences("spuser",
							Context.MODE_PRIVATE);
					Editor editoruser = spuser.edit();
					editoruser.putString("username", et_shoujihao.getText().toString());
					editoruser.putString("pwd", et_password.getText().toString());
					editoruser.commit();

					Intent intent = new Intent(getApplicationContext(),
							JiashiyuanActivity.class);
					startActivity(intent);
					finish();
				}else{
					Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private long exitTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){   
	        if((System.currentTimeMillis()-exitTime) > 2000){  
	            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();                                
	            exitTime = System.currentTimeMillis();   
	        } else {
	            finish();
	            System.exit(0);
	        }
	        return true;   
	    }
	    return super.onKeyDown(keyCode, event);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

}
